package com.wwt.springboot_vue.controller;

import com.wwt.springboot_vue.bean.Goods;
import com.wwt.springboot_vue.service.GoodsService;
import com.wwt.springboot_vue.util.MyFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("goods")
@CrossOrigin
public class GoodsController {
    @Autowired
     private GoodsService goodsService;

    @GetMapping("findallgoods")
    public List<Goods> findAllGoods(){
        List<Goods> list= goodsService.findAllGoods();
        return list;
    }
    @GetMapping("findByLimit")
    public List<Goods> findByLimit(int start,int size){
        List<Goods> list= goodsService.findByLimit(start, size);
        for(int i = 0;i < list.size();i++) {
            list.get(i).setGoods_img(MyFileUtils.getImageStrFromPath(list.get(i).getGoods_img()));
        }
        return list;
    }
    //上传图片专用
    @PostMapping("uploadImage")
    public Map<String,String> uploadImage(MultipartFile file, HttpServletRequest req) {
        Map<String,String> map = new HashMap<>();
        System.out.println(file);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String format = sdf.format(new Date());
        String path = "D:/AShopMimg/xwk/";
        File folder = new File(path);
        System.out.println(file.getOriginalFilename());
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();//用户的原来图片的名字

        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        String url = path + newName;
        System.out.println(url);
        File f1 = new File(url);
        try {
            FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(f1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("url",url);
        String imageStrFromPath = MyFileUtils.getImageStrFromPath(url);
        map.put("base64",imageStrFromPath);
        return map;
    }
    @GetMapping("findbyId")
    public List<Goods> findById(int goods_id){
        List<Goods> list=goodsService.findById(goods_id);
        for(int i = 0;i < list.size();i++) {
            list.get(i).setGoods_img(MyFileUtils.getImageStrFromPath(list.get(i).getGoods_img()));
        }
        System.out.println(list);
        return list;
    }
    @GetMapping("findbylike")
    public List<Goods> findBylike(String query){
        List<Goods> list=goodsService.findByLike(query);
        for(int i = 0;i < list.size();i++) {
            list.get(i).setGoods_img(MyFileUtils.getImageStrFromPath(list.get(i).getGoods_img()));
        }
        System.out.println(list);
        return list;
    }

    @GetMapping("findbylikeLimit")
    public List<Goods> findbylikeLimit(String query,int start,int size){
        List<Goods> list=goodsService.findByLikeLimit(query,start,size);
        for(int i = 0;i < list.size();i++) {
            list.get(i).setGoods_img(MyFileUtils.getImageStrFromPath(list.get(i).getGoods_img()));
        }
        System.out.println(list);
        return list;
    }
    @GetMapping("delete")
    public Map<String,Object>delete(int goods_id){
        Map<String,Object> map = new HashMap<>();
        System.out.println(goods_id);
        try {
            goodsService.delete(goods_id);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }
    @GetMapping("add")
    public Map<String,Object>add(String goods_name,
                                 int goods_price,
                                 int goods_number,
                                 String goods_intro,
                                 String goods_status,
                                 int category_id,
                                 String goods_img){
        Map<String,Object> map = new HashMap<>();

        try {
            goodsService.add( goods_name, goods_price,goods_number, goods_intro,goods_status,category_id,goods_img);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }
    @PostMapping("update")
    public Map<String,Object>update(@RequestBody Goods goods){
        Map<String,Object> map = new HashMap<>();
        System.out.println(goods);
        try {
            goodsService.update(goods);

            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }
}
